package screen.component
{
	import com.adobe.serialization.json.JSON;
	import flash.net.URLRequest;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.text.TextField;
	import flash.utils.setTimeout;
	
	import helper.Reward;
	import helper.Services;
	
	import screen.SacombankScreen;
	
	public class Places extends UI
	{
		private const _ITEM_QUEST_FOLDER:String="../assets/avatar/itemQuest/";
		public const GIRL_MC:String = "girlMc";
		public var _bg:MovieClip;
		private var _get_mission_loader:URLLoader;
		private var _do_mission_loader:URLLoader;
		private var _service:Services;
		private var _place_id:Number;
		private var _mission_ob:Object;
		public function Places()
		{
			_service = Services.getInstance();
			_get_mission_loader = new URLLoader();
			_do_mission_loader = new URLLoader();
		}
		public function getMission(placeId:Number):void{
			if(checkBusy()){
				return;
			}
			_place_id = placeId;
			_get_mission_loader.addEventListener(Event.COMPLETE,getMissionComplete);
			_get_mission_loader.addEventListener(IOErrorEvent.IO_ERROR,getMissionError);
			_get_mission_loader.load(_service.getMissionRequest(placeId));
		}
		private function getMissionComplete(e:Event):void{
			_get_mission_loader.removeEventListener(Event.COMPLETE,getMissionComplete);
			_get_mission_loader.removeEventListener(IOErrorEvent.IO_ERROR,getMissionError);
			_mission_ob = JSON.decode(e.target.data);
			var girl_mc:MovieClip = _bg.getChildByName(GIRL_MC) as MovieClip;
			if (_mission_ob) {
				girl_mc.questionAvatar.load(new URLRequest(_ITEM_QUEST_FOLDER + _mission_ob.questId +".png"));
				TextField(girl_mc.questionTf).text = _mission_ob.message;
				girl_mc.gotoAndStop("question");
				buttonImageInit(girl_mc.yesBtn)
				MovieClip(girl_mc.yesBtn).removeEventListener(MouseEvent.CLICK,missionAgain);
				MovieClip(girl_mc.yesBtn).addEventListener(MouseEvent.CLICK,doMission);
				MovieClip(girl_mc.goHomeBtn).visible = false;
				MovieClip(girl_mc.noBtn).visible = true;
				buttonImageInit(girl_mc.noBtn);
				MovieClip(girl_mc.noBtn).addEventListener(MouseEvent.CLICK,closeMission);
				}
		}
		private function getMissionError(e:IOErrorEvent):void{
			_get_mission_loader.removeEventListener(Event.COMPLETE,getMissionComplete);
			_get_mission_loader.removeEventListener(IOErrorEvent.IO_ERROR,getMissionError);
		}
		private function doMission(e:MouseEvent):void{
			if(checkBusy()){
				return;
			}
			_do_mission_loader.addEventListener(Event.COMPLETE,doMissionComplete);
			_do_mission_loader.addEventListener(IOErrorEvent.IO_ERROR,doMissionError);
			_do_mission_loader.load(_service.doMissionRequest(_mission_ob.questId));
		}
		private function doMissionComplete(e:Event):void{
			_do_mission_loader.removeEventListener(Event.COMPLETE,doMissionComplete);
			_do_mission_loader.removeEventListener(IOErrorEvent.IO_ERROR,doMissionError);
			var result:Number = JSON.decode(e.target.data);
			closeMission();
			var girl_mc:MovieClip = _bg.getChildByName(GIRL_MC) as MovieClip;
			if (result == 1) {
				Reward.getInstance().showOn(girl_mc, SacombankScreen.getInstance().getControl_left.starMc, _mission_ob);
				Resource.getInstance().reduceGold(_mission_ob.gold);
				setTimeout(showMissionAgain,1000);
			}else {
				setTimeout(showNotEnoughMoney,500);
				trace("khong du tien");
			}
			
		}
		private function doMissionError(E:IOErrorEvent):void{
			_do_mission_loader.removeEventListener(Event.COMPLETE,doMissionComplete);
			_do_mission_loader.removeEventListener(IOErrorEvent.IO_ERROR,doMissionError);
			trace("doMissionError");
		}
		private function checkBusy():Boolean{
			if(_get_mission_loader.hasEventListener(Event.COMPLETE) || _do_mission_loader.hasEventListener(Event.COMPLETE)){
				return true;
			}
			return false;
		}
		public function closeMission(e:MouseEvent=null):void{
			var girl_mc:MovieClip = _bg.getChildByName(GIRL_MC) as MovieClip;
			girl_mc.gotoAndStop("stop");
		}
		public function showMission(e:MouseEvent):void{
			/*var girl_mc:MovieClip = _bg.getChildByName(GIRL_MC) as MovieClip;
			girl_mc.gotoAndStop("question");*/
			getMission(_place_id);
		}
		private function showMissionAgain():void{
			var girl_mc:MovieClip = _bg.getChildByName(GIRL_MC) as MovieClip;
			TextField(girl_mc.questionTf).text = "Bé có muốn làm tiếp nhiệm vụ này không?";
			girl_mc.gotoAndStop("question");
			buttonImageInit(girl_mc.yesBtn)
			MovieClip(girl_mc.yesBtn).removeEventListener(MouseEvent.CLICK,doMission);
			MovieClip(girl_mc.yesBtn).addEventListener(MouseEvent.CLICK,missionAgain);
			MovieClip(girl_mc.noBtn).visible = false;
			MovieClip(girl_mc.goHomeBtn).visible = true;;
			buttonImageInit(girl_mc.goHomeBtn);
			MovieClip(girl_mc.goHomeBtn).no = 0;
			MovieClip(girl_mc.goHomeBtn).addEventListener(MouseEvent.CLICK,goHome);
		}
		
		private function showNotEnoughMoney():void {
			var girl_mc:MovieClip = _bg.getChildByName(GIRL_MC) as MovieClip;
			TextField(girl_mc.questionTf).text = "Bé không đủ Xu rồi, hãy về nhà và tiết kiệm thêm Xu nhé!";
			girl_mc.gotoAndStop("question");
			MovieClip(girl_mc.noBtn).visible = false;
			MovieClip(girl_mc.yesBtn).visible = false;
			MovieClip(girl_mc.goHomeBtn).visible = true;;
			buttonImageInit(girl_mc.goHomeBtn);
			girl_mc.goHomeBtn.x = 20;
			MovieClip(girl_mc.goHomeBtn).no = 0;
			MovieClip(girl_mc.goHomeBtn).addEventListener(MouseEvent.CLICK,goHome);
		}
		private function missionAgain(e:MouseEvent):void{
			getMission(_place_id)
		}
		private function goHome(e:MouseEvent):void{
			SacombankScreen.getInstance().sliderPlace(e);
		}
		
	}
}